package com.weimei.conversionTreasure.app;

import android.annotation.SuppressLint;
import android.app.Application;

import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import com.weimei.conversionTreasure.http.Injection;
import com.weimei.conversionTreasure.http.Repository;
import com.weimei.conversionTreasure.viewModel.AddBudgetVM;
import com.weimei.conversionTreasure.viewModel.AddJxsVM;
import com.weimei.conversionTreasure.viewModel.AddKhVM;
import com.weimei.conversionTreasure.viewModel.AddMarketVM;
import com.weimei.conversionTreasure.viewModel.AddPfVM;
import com.weimei.conversionTreasure.viewModel.AddRecordVM;
import com.weimei.conversionTreasure.viewModel.BZJDVM;
import com.weimei.conversionTreasure.viewModel.CBVM;
import com.weimei.conversionTreasure.viewModel.DRVM;
import com.weimei.conversionTreasure.viewModel.GDJSVM;
import com.weimei.conversionTreasure.viewModel.JJJSVM;
import com.weimei.conversionTreasure.viewModel.JJMDVM;
import com.weimei.conversionTreasure.viewModel.JxsDetailVM;
import com.weimei.conversionTreasure.viewModel.JyDetailVM;
import com.weimei.conversionTreasure.viewModel.KhDetailVM;
import com.weimei.conversionTreasure.viewModel.KhRecordVM;
import com.weimei.conversionTreasure.viewModel.LoginVM;
import com.weimei.conversionTreasure.viewModel.MonthDetailVM;
import com.weimei.conversionTreasure.viewModel.PfRecordVM;
import com.weimei.conversionTreasure.viewModel.SaveXlVM;
import com.weimei.conversionTreasure.viewModel.SelKhVM;
import com.weimei.conversionTreasure.viewModel.SetVM;
import com.weimei.conversionTreasure.viewModel.SheRuVM;
import com.weimei.conversionTreasure.viewModel.TJDVM;
import com.weimei.conversionTreasure.viewModel.TJZLVM;
import com.weimei.conversionTreasure.viewModel.TabOneVM;
import com.weimei.conversionTreasure.viewModel.TabThreeVM;
import com.weimei.conversionTreasure.viewModel.TabTwoVM;
import com.weimei.conversionTreasure.viewModel.TotalVM;
import com.weimei.conversionTreasure.viewModel.UpDownVM;
import com.weimei.conversionTreasure.viewModel.YearDetailVM;

/**
 * Created by goldze on 2019/3/26.
 */
public class AppViewModelFactory extends ViewModelProvider.NewInstanceFactory {
    @SuppressLint("StaticFieldLeak")
    private static volatile AppViewModelFactory INSTANCE;
    private final Application mApplication;
    private final Repository mRepository;

    public static AppViewModelFactory getInstance(Application application) {
        if (INSTANCE == null) {
            synchronized (AppViewModelFactory.class) {
                if (INSTANCE == null) {
                    INSTANCE = new AppViewModelFactory(application, Injection.provideRepository());
                }
            }
        }
        return INSTANCE;
    }

    @VisibleForTesting
    public static void destroyInstance() {
        INSTANCE = null;
    }

    private AppViewModelFactory(Application application, Repository repository) {
        this.mApplication = application;
        this.mRepository = repository;
    }

    @NonNull
    @Override
    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
        if (modelClass.isAssignableFrom(LoginVM.class)) {
            return (T) new LoginVM(mApplication, mRepository);
        }else  if (modelClass.isAssignableFrom(SetVM.class)) {
            return (T) new SetVM(mApplication, mRepository);
        }else  if (modelClass.isAssignableFrom(AddRecordVM.class)) {
            return (T) new AddRecordVM(mApplication, mRepository);
        }else  if (modelClass.isAssignableFrom(TabOneVM.class)) {
            return (T) new TabOneVM(mApplication, mRepository);
        }else  if (modelClass.isAssignableFrom(DRVM.class)) {
            return (T) new DRVM(mApplication, mRepository);
        }else  if (modelClass.isAssignableFrom(KhRecordVM.class)) {
            return (T) new KhRecordVM(mApplication, mRepository);
        }else  if (modelClass.isAssignableFrom(AddKhVM.class)) {
            return (T) new AddKhVM(mApplication, mRepository);
        }else  if (modelClass.isAssignableFrom(AddJxsVM.class)) {
            return (T) new AddJxsVM(mApplication, mRepository);
        }else  if (modelClass.isAssignableFrom(KhDetailVM.class)) {
            return (T) new KhDetailVM(mApplication, mRepository);
        }else  if (modelClass.isAssignableFrom(AddMarketVM.class)) {
            return (T) new AddMarketVM(mApplication, mRepository);
        }else  if (modelClass.isAssignableFrom(JxsDetailVM.class)) {
            return (T) new JxsDetailVM(mApplication, mRepository);
        }else  if (modelClass.isAssignableFrom(SaveXlVM.class)) {
            return (T) new SaveXlVM(mApplication, mRepository);
        }else  if (modelClass.isAssignableFrom(TabTwoVM.class)) {
            return (T) new TabTwoVM(mApplication, mRepository);
        }else  if (modelClass.isAssignableFrom(AddBudgetVM.class)) {
            return (T) new AddBudgetVM(mApplication, mRepository);
        }else  if (modelClass.isAssignableFrom(TotalVM.class)) {
            return (T) new TotalVM(mApplication, mRepository);
        }else  if (modelClass.isAssignableFrom(MonthDetailVM.class)) {
            return (T) new MonthDetailVM(mApplication, mRepository);
        }else  if (modelClass.isAssignableFrom(JyDetailVM.class)) {
            return (T) new JyDetailVM(mApplication, mRepository);
        }else  if (modelClass.isAssignableFrom(YearDetailVM.class)) {
            return (T) new YearDetailVM(mApplication, mRepository);
        }else  if (modelClass.isAssignableFrom(SelKhVM.class)) {
            return (T) new SelKhVM(mApplication, mRepository);
        }else  if (modelClass.isAssignableFrom(JJMDVM.class)) {
            return (T) new JJMDVM(mApplication, mRepository);
        }else  if (modelClass.isAssignableFrom(TJDVM.class)) {
            return (T) new TJDVM(mApplication, mRepository);
        }else  if (modelClass.isAssignableFrom(SheRuVM.class)) {
            return (T) new SheRuVM(mApplication, mRepository);
        }else  if (modelClass.isAssignableFrom(BZJDVM.class)) {
            return (T) new BZJDVM(mApplication, mRepository);
        }else  if (modelClass.isAssignableFrom(UpDownVM.class)) {
            return (T) new UpDownVM(mApplication, mRepository);
        }else  if (modelClass.isAssignableFrom(TJZLVM.class)) {
            return (T) new TJZLVM(mApplication, mRepository);
        }else  if (modelClass.isAssignableFrom(JJJSVM.class)) {
            return (T) new JJJSVM(mApplication, mRepository);
        }else  if (modelClass.isAssignableFrom(AddPfVM.class)) {
            return (T) new AddPfVM(mApplication, mRepository);
        }else  if (modelClass.isAssignableFrom(PfRecordVM.class)) {
            return (T) new PfRecordVM(mApplication, mRepository);
        }else  if (modelClass.isAssignableFrom(GDJSVM.class)) {
            return (T) new GDJSVM(mApplication, mRepository);
        }else  if (modelClass.isAssignableFrom(CBVM.class)) {
            return (T) new CBVM(mApplication, mRepository);
        }
        throw new IllegalArgumentException("Unknown ViewModel class: " + modelClass.getName());
    }
}
